﻿namespace DotRas
{
    using DotRas.Design;
    using pptpclient.Properties;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Design;
    using System.Runtime.InteropServices;
    using System.Security;
    using System.Security.Permissions;

    [ToolboxBitmap(typeof(RasEntryDialog), "DotRas.RasEntryDialog.bmp"), ToolboxItem(true)]
    public sealed class RasEntryDialog : RasCommonDialog
    {
        private string _entryName;
        private bool _noRename;
        private string _phoneBookPath;
        private DotRas.RasDialogStyle _rasDialogStyle;

        public override void Reset()
        {
            this.EntryName = null;
            this.NoRename = false;
            this.RasDialogStyle = DotRas.RasDialogStyle.Create;
            this.PhoneBookPath = null;
            base.Reset();
        }

        [UIPermission(SecurityAction.Demand, Window=UIPermissionWindow.SafeSubWindows)]
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            if (string.IsNullOrEmpty(this.PhoneBookPath))
            {
                DotRas.ThrowHelper.ThrowArgumentException("PhoneBookPath", Resources.Argument_StringCannotBeNullOrEmpty);
            }
            DotRas.NativeMethods.RASENTRYDLG lpInfo = new DotRas.NativeMethods.RASENTRYDLG {
                size = Marshal.SizeOf(typeof(DotRas.NativeMethods.RASENTRYDLG)),
                hwndOwner = hwndOwner
            };
            if (this.RasDialogStyle == DotRas.RasDialogStyle.Edit)
            {
                if (this.NoRename)
                {
                    lpInfo.flags |= DotRas.NativeMethods.RASEDFLAG.NoRename;
                }
            }
            else
            {
                lpInfo.flags |= DotRas.NativeMethods.RASEDFLAG.NewEntry;
            }
            if (base.Location != Point.Empty)
            {
                lpInfo.left = base.Location.X;
                lpInfo.top = base.Location.Y;
                lpInfo.flags |= DotRas.NativeMethods.RASEDFLAG.PositionDlg;
            }
            bool flag = false;
            try
            {
                new FileIOPermission(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, this.PhoneBookPath).Demand();
                string lpszEntryName = null;
                if (!string.IsNullOrEmpty(this.EntryName))
                {
                    lpszEntryName = this.EntryName;
                }
                flag = DotRas.UnsafeNativeMethods.RasEntryDlg(this.PhoneBookPath, lpszEntryName, ref lpInfo);
                if (flag)
                {
                    this.EntryName = lpInfo.entryName;
                }
            }
            catch (EntryPointNotFoundException)
            {
                DotRas.ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
            }
            catch (SecurityException)
            {
                DotRas.ThrowHelper.ThrowUnauthorizedAccessException(Resources.Exception_AccessDeniedBySecurity);
            }
            return flag;
        }

        [DefaultValue((string) null), DotRas.SRCategory("CatData"), DotRas.SRDescription("REDEntryNameDesc")]
        public string EntryName
        {
            get
            {
                return this._entryName;
            }
            set
            {
                this._entryName = value;
            }
        }

        [DotRas.SRDescription("REDNoRenameDesc"), DotRas.SRCategory("CatBehavior"), DefaultValue(false)]
        public bool NoRename
        {
            get
            {
                return this._noRename;
            }
            set
            {
                this._noRename = value;
            }
        }

        [DefaultValue((string) null), DotRas.SRCategory("CatData"), Editor(typeof(PhoneBookFileNameEditor), typeof(UITypeEditor)), DotRas.SRDescription("REDPhoneBookPathDesc")]
        public string PhoneBookPath
        {
            get
            {
                return this._phoneBookPath;
            }
            set
            {
                this._phoneBookPath = value;
            }
        }

        [DotRas.SRDescription("REDRasDialogStyleDesc"), DefaultValue(typeof(DotRas.RasDialogStyle), "Create"), DotRas.SRCategory("CatBehavior")]
        public DotRas.RasDialogStyle RasDialogStyle
        {
            get
            {
                return this._rasDialogStyle;
            }
            set
            {
                this._rasDialogStyle = value;
            }
        }
    }
}

